/*
 * @Author: 阿芙
 * @Date: 2021-04-14 19:05:09
 * @LastEditTime: 2021-04-17 20:56:33
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: /ManageTemplate/public/ruoling-request/js/ruoling-request-Request.js
 */





/**
 * @description: 简单的ajax请求，开启：一次有效请求，不能并行
 * @param {*} layer
 * @param {*} jquery
 * @param {*} function
 * @return {*}
 */
layui.define(["layer","jquery"],function(exports){
    const $ = layui.jquery;
    const layer  = layui.layer;
    let AjaxStatus = true;  //设置ajax当前状态(是否可以发送);
  
    /**
     * @description: 构造
     * @param {*}
     * @return {*}
     */
    class Yiling_Ajax{
        constructor(){
            "use strict";
            this.Type = "GET";
            this.Url = null;
            this.Timeout = null;
            this.DataPara = null;
            this.DataType = "json";
            this.ShowSwitch = true;  //Request信息是否返回
            this.CloseLoading = null;
            this.alone = false; // true 一次有效请求，false，可以执行多个请求
            this.msg = "数据加载中...";
            this._options = {
    
            }
        }
    };
    
    /**
     * @description: 请求类型
     * @param {*} type GET/POST
     * @return {*}
     */
    Yiling_Ajax.prototype.SetType = function (type) {
        "use strict";
        this.Type = type;
        return this;
    };
    
   /**
     * @description: URL地址
     * @param {*} url
     * @return {*}
     */
    Yiling_Ajax.prototype.SetUrl = function (url){
        "use strict";
        this.Url = url;
        return this;
    };
    
    /**
     * @description: 超时时间
     * @param {*} timeout
     * @return {*}
     */
    Yiling_Ajax.prototype.SetTimeout = function (timeout) {
        "use strict";
        this.Timeout = timeout;
        return this;
    };
    
    /**
     * @description: Set参数
     * @param {*} data
     * @return {*}
     */
    Yiling_Ajax.prototype.SetDataPara = function (data) {
        "use strict";
        this.DataPara = data;
        return this;
    };
    
    /**
     * @description: 返回数据类型 例如 json
     * @param {*} datatype
     * @return {*}
     */
    Yiling_Ajax.prototype.SetDataType = function (datatype) {
        this.DataType = datatype;
        return this;
    }

    /**
     * @description: 单独执行一次
     * @param {*}
     * @return {*}
     */
    Yiling_Ajax.prototype.Alone = function () {
        this.alone = true;
        return this;
    };

    
    /**
     * @description: 默认执行调用
     * @param {*} callback
     * @return {*}
     */
    Yiling_Ajax.prototype.Init = function (callback) {
        "use strict";
        let _this = this;
        if(typeof callback != "function"){
            callback = function(){};
        }
        _this._options = {
            Type:_this.Type,
            Url:_this.Url,
            DataPara:_this.DataPara,
            DataType:_this.DataType,
        }
        if(_this.alone){  //开启有效的一次请求
            if(AjaxStatus){
                AjaxStatus = false;
                setTimeout(()=>{
                    if(_this.ShowSwitch){
                        _this.ShowSwitch = true;
                        _this.CloseLoading = layer.msg(_this.msg, {icon: 16, shade: 0.3, time:0});  
                    }
                },300)
                _this.Ajax(function(data){
                    callback(data);
                })
            }
        }else{ //默认可以使用有效的多次请求
            setTimeout(()=>{
                if(_this.ShowSwitch){
                    _this.ShowSwitch = true;
                    _this.CloseLoading = layer.msg(_this.msg, {icon: 16, shade: 0.3, time:0});  
                }
            },300)
            _this.Ajax(function(data){
                callback(data);
            })
        }     
        return _this;
    };
    
    /**
     * @description: 配置
     * @param {*} callback
     * @return {*}
     */
    Yiling_Ajax.prototype.Ajax = function (callback) {
        
        let _this = this;
        const starttime = new Date().getTime();
        $.ajax({
            type:_this._options.Type,
            url:_this._options.Url,
            data:_this._options.DataPara,
            dataType:_this._options.DataType,
            success:function(data){
                _this.ShowSwitch = false;
                //计算请求发起至返回数据间隔时间（毫秒）超过1秒钟不再延迟加载数据，没超过1秒钟将延迟加载，防止弹出加载框秒现秒隐
                const endtime = new Date().getTime();
                let Plustime = endtime - starttime;
                let Tout = 0; //延迟处理时间
               
                //如果在300毫秒内没有数据返回，防止有时弹出框一闪而逝，至少1000毫秒后再隐藏
                if(typeof _this.CloseLoading == "number" && Plustime < 1000){
                    Tout = 1000;
                }
               
                setTimeout(()=>{
                    AjaxStatus = true;
                    if(_this.CloseLoading){
                        layer.close(_this.CloseLoading);
                        _this.CloseLoading = null;
                    }
                    //成功回调
                    if(data.ErrorCode == 0){

                        //layer.msg(data.ErrorMessage);
                        if(data.ErrorMessage.length != 0){
                            layer.msg(data.ErrorMessage,{icon:1,time:1000});
                            setTimeout(()=>{
                                callback(data);
                            },1000)
                        }else{
                            callback(data);
                        }

                    }else if(data.ErrorCode == 600101){

                        layer.open({
                            type: 1
                            ,content: '<div style="padding: 20px 100px;">'+ data.ErrorMessage +'</div>'
                            ,btn: '确认'
                            ,btnAlign: 'c' //按钮居中
                            ,shade: 0.3 //不显示遮罩
                            ,yes: function(){
                                layer.closeAll();
                            }
                        });

                    }else if(data.ErrorCode == 600102) {

                        layer.open({
                            type: 1
                            , content: '<div style="padding: 20px 100px;">' + data.ErrorMessage + '</div>'
                            , btn: '确认'
                            , btnAlign: 'c' //按钮居中
                            , shade: 0.3 //不显示遮罩
                            , yes: function () {
                                layer.closeAll();
                            }
                        });

                    }else{
                        layer.msg(data.ErrorMessage);
                    }
                },Tout);
            },
            error:function(){
                AjaxStatus = true;
            }
        });
    };


    /**
     * @description: 外部调用方法
     * @param {*}
     * @return {*}
     */
    const Yiling = {
        v:"1.0.1",
        YI_Ajax:Yiling_Ajax  //ajax封装请求
    }

    /**
     * @description: 抛出去
     * @param {*}
     * @return {*}
     */
    exports("Yiling",Yiling);

})


